package com.example.testandroid;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.*;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.Spshare.SPActivity;

public class MainActivity extends Activity {

	
	List<String> item_names = new ArrayList<String>();
	List<Integer> item_imgs = new ArrayList<Integer>();
	
	private TextView tv;
	private Button btn;
	FragmentDemo fragment1 = new FragmentDemo();
	Fragment fragment2 = new Frag1();
	boolean b = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.e("tag","MainActivity:onCreate");
		
		tv = (TextView)findViewById(R.id.tv);
		btn = (Button)findViewById(R.id.btn);
		
		Intent i= new Intent(this,MyService.class);
		startService(i);
//		MyService.context = this;




		btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				tv.setText("add");
				FragmentManager fragmentManager =getFragmentManager();
			    FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
				if(b){
					//fragmentTransaction.remove(fragment2);
					//fragmentTransaction.replace(R.id.frg_container, fragment1);
					fragmentTransaction.add(R.id.frg_container,fragment1);
					b=!b;
					startActivity(new Intent(MainActivity.this,RoboGuiceDemoActivity.class));
				}else{
					//fragmentTransaction.remove(fragment1);
					//fragmentTransaction.replace(R.id.frg_container, fragment2);
					fragmentTransaction.add(R.id.frg_container,fragment2);
					b=!b;
				}


				fragmentTransaction.commit();

			}
			
		});
		
		GridView gv = (GridView)findViewById(R.id.gv);
		gv.setNumColumns(4);
		gv.setBackgroundColor(Color.TRANSPARENT);
		
		
		item_names.add("item1");
		item_names.add("item2");
		item_names.add("item3");
		//item_names.add("item4");
		//item_names.add("item5");
		
		//item_imgs.add(R.drawable.ic_launcher);
		//item_imgs.add(R.drawable.ic_launcher);
		item_imgs.add(R.drawable.ic_launcher);
		item_imgs.add(R.drawable.ic_launcher);
		item_imgs.add(R.drawable.ic_launcher);
		
		MyAdapter ap = new MyAdapter(this, item_names, item_imgs);
		
		gv.setAdapter(ap);
		gv.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "item:"+item_names.get(arg2), Toast.LENGTH_SHORT).show();
			}
			
		});
		
		//send broadcast btn
		Button btn1 = (Button)findViewById(R.id.btn1);
		Button btn2 = (Button)findViewById(R.id.btn2);
		
		btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();  
                intent.setAction("com.byread.static");  
                intent.putExtra("msg", "接收静态注册广播成功！");  
                sendBroadcast(intent);  
			}
		});
		
		btn2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();  
                intent.setAction(Intent.ACTION_POWER_CONNECTED);  
                intent.putExtra("msg", "正在充电。。。。"); 
                //sendBroadcast(intent);  
			}
		});
		
		
		
		//switch btn
		Button swButton = (Button)findViewById(R.id.switch_btn);
		swButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(MainActivity.this,SPActivity.class));
			}
		});
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		Toast.makeText(this, "menu is created!", Toast.LENGTH_SHORT).show();
		
		menu.add(1, Menu.FIRST+1, 5, "add").setIcon(android.R.drawable.ic_menu_add);
		menu.add(1, Menu.FIRST+2, 1, "delete").setIcon(android.R.drawable.ic_menu_delete);
		menu.add(2, Menu.FIRST+3, 1, "help").setIcon(android.R.drawable.ic_menu_help);
		menu.add(2, Menu.FIRST+4, 1, "edit").setIcon(android.R.drawable.ic_menu_edit);
		menu.add(2, Menu.FIRST+5, 1, "move").setIcon(android.R.drawable.ic_menu_manage);
		
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		case Menu.FIRST+1:
			Toast.makeText(MainActivity.this, "add", Toast.LENGTH_SHORT).show();
		break;
		case Menu.FIRST+2:
			Toast.makeText(MainActivity.this, "delete", Toast.LENGTH_SHORT).show();
		break;
		case Menu.FIRST+3:
			Toast.makeText(MainActivity.this, "select", Toast.LENGTH_SHORT).show();
		break;
		case Menu.FIRST+4:
			Toast.makeText(MainActivity.this, "edit", Toast.LENGTH_SHORT).show();
		break;
		case Menu.FIRST+5://default:
			Toast.makeText(MainActivity.this, "move", Toast.LENGTH_SHORT).show();
		break;
		}
		return false;
	}
	
	@Override
	public boolean onMenuOpened(int featureId,Menu menu){
		Toast.makeText(this, "menu is opened!", Toast.LENGTH_SHORT).show();
		return super.onMenuOpened(featureId, menu);
		
	}
	
	@Override
	public void onOptionsMenuClosed(Menu menu){
		Toast.makeText(this, "menu is on closed!", Toast.LENGTH_SHORT).show();
		super.onOptionsMenuClosed(menu);
	}
	
	
	
	
	  @Override  
	    protected void onStart() {  
	        super.onStart();
	        Log.e("tag","MainActivity:onStart");
	       
	        // 注册自定义动态广播消息  
	        IntentFilter filter_dynamic = new IntentFilter();  
	        filter_dynamic.addAction("com.byread.static");  
	        registerReceiver(dynamicReceiver, filter_dynamic);  
	        // 注册系统动态广播消息  
	        IntentFilter filter_system = new IntentFilter();  
	        filter_system.addAction(Intent.ACTION_POWER_CONNECTED);  
	       // registerReceiver(systemReceiver, filter_system);  
	    }  
	    private BroadcastReceiver dynamicReceiver = new BroadcastReceiver() {  
	          
	        @Override  
	        public void onReceive(Context context, Intent intent) {  
	            
	           // if(intent.getAction().equals("com.byread.static")){  
	                String msg = intent.getStringExtra("msg");  
	                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();  
	            //}  
	        }  
	    };  
	    private BroadcastReceiver systemReceiver = new BroadcastReceiver() {  
	          
	        @Override  
	        public void onReceive(Context context, Intent intent) {  
	            
	            if(intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)){  
	                String msg = intent.getStringExtra("msg");  
	                Toast.makeText(context, "充电中...", Toast.LENGTH_SHORT).show();  
	            }  
	        }  
	    };
	    protected void onPause(){
	    	super.onPause();
	    	
	    	Log.e("tag","MainActivity:onPause");
	    }
	
	    protected void onResume(){
	    	super.onResume();
	    	Log.e("tag","MainActivity:onResume");
	    }
	
	    protected void onRestart(){
	    	super.onRestart();
	    	Log.e("tag","MainActivity:onRestart");
	    }
	    
	    protected void onDestroy(){
	    	super.onDestroy();
	    	Log.e("tag","MainActivity:onDestroy");
	    	unregisterReceiver(dynamicReceiver);
	    }
	
	
	    protected void onStop(){
	    	super.onStop();
	    	Log.e("tag", "MainActivity:onStop");
	    }
	
	
	
	
	
	
	
	
	
}
